
#include "dlfcn.h"
#include <stdio.h>
#include <iostream>

void OnInit()
{
}
void OnUpdate()
{
}
void OnGUI()
{
}
void OnExit()
{
}
void TestMainLoop()
{
    auto filename = "./libTTEngine.Native.so";
    //auto filename = "../../fakeunity.native.player/out/libbgfx-shared-libRelease.so";
    FILE *file = fopen(filename, "rt");
    if (file == NULL)
    {
        printf("dile not exist.");
    }
    else
    {
        fclose(file);
    }
    void *handle = dlopen(filename, RTLD_NOW);
    if (handle == NULL)
    {
        const char *dlerr = dlerror();
        printf(dlerr);
    }
    else
    {
        typedef void (*deleOnInit)();
        typedef void (*deleOnUpdate)();
        typedef void (*deleOnGUI)();
        typedef void (*deleOnExit)();
        typedef int (*deleApp_MainLoop)(const char16_t *title, deleOnInit onInit, deleOnUpdate onUpdate, deleOnGUI onGUI, deleOnExit onExit);
        deleApp_MainLoop App_MainLoop = (deleApp_MainLoop)dlsym(handle, "App_MainLoop");
        App_MainLoop(u"hello", OnInit, OnUpdate, OnGUI, OnExit);
    }
}
void TestNFD()
{
    auto filename = "./libTTEngine.Native.Editor.so";
    FILE *file = fopen(filename, "rt");
    if (file == NULL)
    {
        printf("dile not exist.");
    }
    else
    {
        fclose(file);
    }

    void *handle = dlopen(filename, RTLD_NOW);
    if (handle == NULL)
    {
        const char *dlerr = dlerror();
        printf(dlerr);
    }
    else
    {
        typedef enum
        {
            NFD_ERROR, /* programmatic error */
            NFD_OKAY,  /* user pressed okay, or successful return */
            NFD_CANCEL /* user pressed cancel */
        } nfdresult_t;

        typedef nfdresult_t (*deleNFD_OpenDialog)(const char *filterList, const char *defaultPath, char **outPath);
        deleNFD_OpenDialog NFD_OpenDialog = (deleNFD_OpenDialog)dlsym(handle, "NFD_OpenDialog");
        char *path = NULL;
        NFD_OpenDialog("*.png", "./", &path);
    }
}
int main(int argc, const char **argv)
{
    TestNFD();
    TestMainLoop();
}